﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

class NetCommandDefine
{
    public enum NetReceviceLevelProtocol
    {
        NET_HEAD_FROM = 0x00,
        NET_HEAD_LENGTH = 0x02,

        NET_FUNC_FROM = NET_HEAD_FROM + NET_HEAD_LENGTH,
        NET_FUNC_LENGTH = 1,

        NET_DEVICE_ID_FROM = NET_FUNC_FROM + NET_FUNC_LENGTH,
        NET_DEVICE_ID_LENGTH = 0x02,

        NET_DEVICE_TYPE_FROM = NET_DEVICE_ID_FROM + NET_DEVICE_ID_LENGTH,
        NET_DEVICE_TYPE_LENGTH = 0x02,

        NET_PROTOCOLDATA_LENGTH_FROM = NET_DEVICE_TYPE_FROM + NET_DEVICE_TYPE_LENGTH,
        NET_PROTOCOLDATA_LENGTH_LENGTH = 0x04,

        NET_PROTOCOLDATA_DATA_FROM = NET_PROTOCOLDATA_LENGTH_FROM + NET_PROTOCOLDATA_LENGTH_LENGTH,


        NET_TAIL_LENGTH = 2,
    }
    public enum NetReceviceLevelProtocolValue
    {
        VALUE_NET_HEAD = 0xFF,
        VALUE_NET_TAIL = 0x0D,
        FUNC_SEND = 0x01
    }

    public static byte GetHeadValue()
    {
        return (byte)(NetReceviceLevelProtocolValue.VALUE_NET_HEAD);
    }
    public static byte GetTailValue()
    {
        return (byte)(NetReceviceLevelProtocolValue.VALUE_NET_TAIL);
    }
    public static int GetDataFrom()
    {
        return (int)NetReceviceLevelProtocol.NET_PROTOCOLDATA_DATA_FROM;
    }
    public static int GetHeadAndTailLength()
    {
        return (int)NetReceviceLevelProtocol.NET_TAIL_LENGTH + (int)NetReceviceLevelProtocol.NET_PROTOCOLDATA_DATA_FROM;
    }
    public static int GetHeadLength()
    {
        return (int)NetReceviceLevelProtocol.NET_PROTOCOLDATA_DATA_FROM;
    }
    public static int GetTailLength()
    {
        return (int)NetReceviceLevelProtocol.NET_TAIL_LENGTH;
    }
    public static void SetHead(ref byte[] bSendData)
    {
        int i;
        for (i = 0; i < (int)(NetReceviceLevelProtocol.NET_HEAD_LENGTH); ++i)
        {
            bSendData[i + (int)(NetReceviceLevelProtocol.NET_HEAD_FROM)] = GetHeadValue();
        }
    }
    public static void SetTail(ref byte[] bSendData)
    {
        uint iLength = (uint)CDataConvert.GetValue(ref bSendData,
            (int)(NetReceviceLevelProtocol.NET_PROTOCOLDATA_LENGTH_FROM),
            (int)(NetReceviceLevelProtocol.NET_PROTOCOLDATA_LENGTH_LENGTH)) + (uint)GetDataFrom();
        int i;
        for (i = 0; i < (int)(NetReceviceLevelProtocol.NET_TAIL_LENGTH); ++i)
        {
            bSendData[i + iLength] = GetTailValue();
        }
    }

    public static void SetDeviceID(ref byte[] bSendData, uint iDeviceID)
    {
        CDataConvert.SetValue(iDeviceID, ref bSendData, (int)(NetReceviceLevelProtocol.NET_DEVICE_ID_FROM), (int)(NetReceviceLevelProtocol.NET_DEVICE_ID_LENGTH));

    }
    public static void SetDeviceType(ref byte[] bSendData, uint iDeviceType)
    {
        CDataConvert.SetValue(iDeviceType, ref bSendData, (int)(NetReceviceLevelProtocol.NET_DEVICE_TYPE_FROM), (int)(NetReceviceLevelProtocol.NET_DEVICE_TYPE_LENGTH));

    }
    public static void SetLength(ref byte[] bSendData, uint iLength)
    {
        CDataConvert.SetValue(iLength, ref bSendData, (int)(NetReceviceLevelProtocol.NET_PROTOCOLDATA_LENGTH_FROM), (int)(NetReceviceLevelProtocol.NET_PROTOCOLDATA_LENGTH_LENGTH));
    }
    public static void SetSendFunc(ref byte[] bSendData)
    {
        CDataConvert.SetValue((uint)(NetReceviceLevelProtocolValue.FUNC_SEND), ref bSendData, (int)(NetReceviceLevelProtocol.NET_FUNC_FROM), (int)(NetReceviceLevelProtocol.NET_FUNC_LENGTH));
    }
    public static bool IsHeadValueTrue(ref byte[] bSendData)
    {
        int i;
        bool ret = true;
        for (i = 0; i < (int)(NetReceviceLevelProtocol.NET_HEAD_LENGTH); ++i)
        {
            if (bSendData[i + (int)(NetReceviceLevelProtocol.NET_HEAD_FROM)] != GetHeadValue())
            {
                ret = false;
                break;
            }
        }
        return ret;
    }
    public static bool IsTailValueTrue(ref byte[] bSendData)
    {
        int i;
        bool ret = true;
        uint iLength = (uint)CDataConvert.GetValue(ref bSendData,
            (int)(NetReceviceLevelProtocol.NET_PROTOCOLDATA_LENGTH_FROM),
            (int)(NetReceviceLevelProtocol.NET_PROTOCOLDATA_LENGTH_LENGTH)) + (uint)GetDataFrom();
        for (i = 0; i < (int)(NetReceviceLevelProtocol.NET_TAIL_LENGTH); ++i)
        {
            if (bSendData[i + iLength] != GetTailValue())
            {
                ret = false;
                break;
            }
        }
        return ret;
    }
    public static NetReceviceLevelProtocolValue GetFunction(ref byte[] bSendData)
    {
        return (NetReceviceLevelProtocolValue)CDataConvert.GetValue(ref bSendData, (int)(NetReceviceLevelProtocol.NET_FUNC_FROM), (int)(NetReceviceLevelProtocol.NET_FUNC_LENGTH));

    }

    public static int GetDeviceID(ref byte[] bSendData)
    {
        return (int)CDataConvert.GetValue(ref bSendData, (int)(NetReceviceLevelProtocol.NET_DEVICE_ID_FROM), (int)(NetReceviceLevelProtocol.NET_DEVICE_ID_LENGTH));

    }
    public static int GetDeviceType(ref byte[] bSendData)
    {
        return (int)CDataConvert.GetValue(ref bSendData, (int)(NetReceviceLevelProtocol.NET_DEVICE_TYPE_FROM), (int)(NetReceviceLevelProtocol.NET_DEVICE_TYPE_LENGTH));

    }
    public static int GetDataLength(ref byte[] bSendData)
    {
        return (int)CDataConvert.GetValue(ref bSendData, (int)(NetReceviceLevelProtocol.NET_PROTOCOLDATA_LENGTH_FROM), (int)(NetReceviceLevelProtocol.NET_PROTOCOLDATA_LENGTH_LENGTH));

    }
    public static void PrepareNetSendData(out byte[] bSendData, byte[] bData, int iFrom, int iLength, uint idDevice = 1000, uint iDeviceType = 2)
    {
        bSendData = new byte[iLength + GetHeadAndTailLength()];
        Array.Copy(bData, iFrom, bSendData, GetDataFrom(), iLength);
        SetHead(ref bSendData);
        SetSendFunc(ref bSendData);
        SetDeviceID(ref bSendData, idDevice);
        SetDeviceType(ref bSendData, iDeviceType);
        SetLength(ref bSendData, (uint)iLength);
        SetTail(ref bSendData);
    }
}
